package cn.wit.front.service.impl;

import cn.wit.front.condition.ContestCondition;
import cn.wit.front.mapper.ContestMapper;
import cn.wit.front.service.ContestService;
import cn.wit.utils.pojo.Contest;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ContestServiceImpl implements ContestService {
    @Autowired
    private ContestMapper contestMapper;


    @Override
    public Result queryContest(IPage<Contest> page, ContestCondition condition) {
        QueryWrapper<Contest> wrapper = new QueryWrapper<>();
        if (condition != null) {
            if (condition.getContestName() != null && condition.getContestName() != "") {
                wrapper.like("contest_name", condition.getContestName());
            }
            if (condition.getContestType() != null && condition.getContestType() != "") {
                wrapper.like("contest_type", condition.getContestType());
            }
            if (condition.getTimeType() != null && condition.getTimeType() != "") {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                switch (condition.getTimeType()) {
                    case "first"://查询全部的竞赛信息
                        break;
                    case "second"://报名中
                        wrapper.le("entry_begin_time", sdf.format(date));
                        wrapper.ge("entry_end_time", sdf.format(date));
                        break;
                    case "third"://进行中
                        wrapper.le("begin_time", sdf.format(date));
                        wrapper.ge("end_time", sdf.format(date));
                        break;
                    case "fourth"://往期比赛
                        wrapper.lt("end_time", sdf.format(date));
                        break;
                }
            }
        }
        IPage<Contest> contestIPage = contestMapper.selectPage(page, wrapper);
        Result result = Result.ok(contestIPage);
        return result;
    }

}
